package com.wbs.controller;

import com.wbs.model.TbItem;
import com.wbs.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: 芝梦
 * @Description: 库存控制层
 * @DateTime: 2024/2/26 21:13
 */
@RequestMapping("item")
@RestController
public class ItemController {
    @Autowired
    private ItemService itemService;

    //通过id查询商品信息
    @GetMapping("selectBySkuId")
    public TbItem selectBySkuId(@RequestParam Long skuId){
        return itemService.selectByPrimaryKey(skuId);
    }

    //更新库存数量
    @PostMapping("updateStockQuantities")
    public void updateStockQuantities(@RequestBody List<TbItem> items){
        for (TbItem item : items) {
            //通过id查询商品信息
            TbItem tbItem = itemService.selectByPrimaryKey(item.getId());
            tbItem.setStockCount(tbItem.getStockCount()-item.getStockCount());
            //更新
            itemService.updateStockQuantities(tbItem);
        }
    }
}
